package com.example.su_shi_tan_bas_lib.album.ui;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.example.library_common.album.ui.AlbumFragment;
import com.example.su_shi_tan_bas_lib.R;
import com.example.su_shi_tan_bas_lib.album.bean.FunctionBean;
import com.example.su_shi_tan_bas_lib.album.interfaces.AlbumPanelNode;
import com.example.su_shi_tan_bas_lib.base.DialogFragmentEx;

import me.yokeyword.fragmentation.ISupportFragment;

public class AlbumPanel extends DialogFragmentEx
        implements AlbumPanelNode,  DialogInterface.OnKeyListener {

    public static AlbumPanel newInstance() {
        Bundle args = new Bundle();
        AlbumPanel fragment = new AlbumPanel();
        fragment.setArguments(args);
        return fragment;
    }

    private FunctionBean functionBean;

    public void setFunctionBean(FunctionBean functionBean) {
        this.functionBean = functionBean;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlbumDialog albumDialog = new AlbumDialog(requireContext());
        albumDialog.setOnKeyListener(this);
        return albumDialog;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.album_panel_layout, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
//        view.setOnKeyListener(this);
        loadRootFragment(R.id.mContainer, AlbumFragment.newInstance());
    }


    @Override
    public boolean onBackPressedSupport() {
        boolean b = super.onBackPressedSupport();
        if (!b){
            dismiss();
        }
        return true;
    }

    @Override
    public FunctionBean getFunctionBean() {
        return functionBean;
    }

    @Override
    public void onBackPressed() {
        onBackPressedSupport();
    }

    @Override
    public void finish() {
        dismiss();
    }

//    @Override
//    public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
//
//        return  onBackPressedSupport();
//    }

    @Override
    public boolean onKey(DialogInterface dialogInterface, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            ISupportFragment topChildFragment = getTopChildFragment();
            boolean flag=false;
            if (topChildFragment != null) {
                flag =topChildFragment.onBackPressedSupport();
            }
            if (!flag){
                onBackPressedSupport();
            }
            return true;
//            return onBackPressedSupport();
        }
        return false;
    }


}
